﻿using Microsoft.Practices.Prism.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    [DataContract]
    public class WPersonnel : NotificationObject
    {
        private int personnelUID;
        private String id;
        private String password;
        private int personnelGroupUID;
        private String name;
        private String head;
        private String mobile;
        private String webChat;
        private String createTime;

        [DataMember(Order = 0)]
        public int PersonnelUID
        {
            get
            {
                return personnelUID;
            }
            set
            {
                personnelUID = value;
                RaisePropertyChanged("PersonnelUID");
            }
        }

        [DataMember(Order = 1)]
        public String ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                RaisePropertyChanged("ID");
            }
        }

        [DataMember(Order = 2)]
        public String Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
                RaisePropertyChanged("Password");
            }
        }

        [DataMember(Order = 3)]
        public int PersonnelGroupUID
        {
            get
            {
                return personnelGroupUID;
            }
            set
            {
                personnelGroupUID = value;
                RaisePropertyChanged("PersonnelGroupUID");
            }
        }

        [DataMember(Order = 4)]
        public String Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                RaisePropertyChanged("Name");
            }
        }

        [DataMember(Order = 5)]
        public String Head
        {
            get
            {
                return head;
            }
            set
            {
                head = value;
                RaisePropertyChanged("Head");
            }
        }

        [DataMember(Order = 6)]
        public String Mobile
        {
            get
            {
                return mobile;
            }
            set
            {
                mobile = value;
                RaisePropertyChanged("Mobile");
            }
        }

        [DataMember(Order = 7)]
        public String WebChat
        {
            get
            {
                return webChat;
            }
            set
            {
                webChat = value;
                RaisePropertyChanged("WebChat");
            }
        }

        [DataMember(Order = 8)]
        public String CreateTime
        {
            get
            {
                return createTime;
            }
            set
            {
                createTime = value;
                RaisePropertyChanged("CreateTime");
            }
        }

        public WPersonnel()
        {
            PersonnelUID = -1;
            ID = "";
            Password = "";
            PersonnelGroupUID = -1;
            Name = "";
            Head = "";
            Mobile = "";
            WebChat = "";
            CreateTime = DateTime.Now.ToString();
    }

        public WPersonnel(LPersonnel p)
        {
            PersonnelUID = p.personnelUID;
            ID = p.ID;
            Password = p.Password;
            Name = p.Name;
            Head = p.Head;
            Mobile = p.Mobile;
            WebChat = p.webChat;
            CreateTime = p.CreateTime;
            PersonnelGroupUID = WPersonnelGroup.GetPersonnelGroupUID(p.PersonnelGroupSelected);
        }

        public static List<WPersonnel> GetAll()
        {
            HttpResponse response = FridgeConnector.PostMessage(EStudy.Personnel, EMethod.SelectAll, "");
            if (response.Status == "Success")
            {
                return JSON.parse<List<WPersonnel>>(response.Data1);
            }

            return null;
        }

        public static List<WPersonnel> Select(List<WPersonnel> pList)
        {
            String postData = JSON.stringify(pList);
            HttpResponse response = FridgeConnector.PostMessage(EStudy.Personnel, EMethod.Insert, postData);
            if (response.Status == "Success")
            {
                return JSON.parse<List<WPersonnel>>(response.Data1);
            }

            return null;
        }

        public static HttpResponse Insert(List<WPersonnel> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.Personnel, EMethod.Insert, postData);
        }

        public static HttpResponse Update(List<WPersonnel> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.Personnel, EMethod.Update, postData);
        }

        public static HttpResponse Delete(List<WPersonnel> pList)
        {
            String postData = JSON.stringify(pList);
            return FridgeConnector.PostMessage(EStudy.Personnel, EMethod.Delete, postData);
        }
    }
}
